package com.example.project.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.example.project.entity.Position;

/**
 * 岗位信息 数据层
 * 
 * @author ruoyi
 */
@Mapper
public interface PositionMapper {

	/**
	 * 查询岗位数据集合
	 * 
	 * @param post 岗位信息
	 * @return 岗位数据集合
	 */
	@Select("<script>select post_id, post_code, post_name, post_sort, status, create_by, create_time, remark from t_position"
			+ "<where>"
			+ "<if test=\"postCode != null and postCode != ''\">AND post_code like concat('%', #{postCode}, '%')</if>"
			+ "<if test=\"status != null and status != ''\">AND status = #{status}</if>"
			+ "<if test=\"postName != null and postName != ''\">AND post_name like concat('%', #{postName}, '%')</if>"
			+ "</where></script>")
	public List<Position> selectPostList(Position post);

	/**
	 * 查询所有岗位
	 * 
	 * @return 岗位列表
	 */
	@Select("select post_id, post_code, post_name, post_sort, status, create_by, create_time, remark from t_position")
	public List<Position> selectPostAll();

	/**
	 * 根据用户ID查询岗位
	 * 
	 * @param userId 用户ID
	 * @return 岗位列表
	 */
	@Select("SELECT p.post_id, p.post_name, p.post_code " + "FROM t_user u "
			+ "LEFT JOIN t_user_position up ON u.user_id = up.user_id "
			+ "LEFT JOIN t_position p ON up.post_id = p.post_id " + "WHERE up.user_id = #{userId}")
	public List<Position> selectPostsByUserId(Long userId);

	/**
	 * 通过岗位ID查询岗位信息
	 * 
	 * @param postId 岗位ID
	 * @return 角色对象信息
	 */
	@Select("select post_id, post_code, post_name, post_sort, status, create_by, create_time, remark from t_position where post_id = #{postId}")
	public Position selectPostById(Long postId);

	/**
	 * 批量删除岗位信息
	 * 
	 * @param ids 需要删除的数据ID
	 * @return 结果
	 */
	@Delete("<script>delete from t_position where post_id in <foreach collection='array' item='postId' open='(' separator=',' close=')'>#{postId}</foreach></script>")
	public int deletePostByIds(Long[] ids);

	/**
	 * 修改岗位信息
	 * 
	 * @param post 岗位信息
	 * @return 结果
	 */
	@Update("<script>update t_position" + "<set>"
			+ "<if test=\"postCode != null and postCode != ''\">post_code = #{postCode},</if>"
			+ "<if test=\"postName != null and postName != ''\">post_name = #{postName},</if>"
			+ "<if test=\"postSort != null and postSort != ''\">post_sort = #{postSort},</if>"
			+ "<if test=\"status != null and status != ''\">status = #{status},</if>"
			+ "<if test=\"remark != null\">remark = #{remark},</if>"
			+ "<if test=\"updateBy != null and updateBy != ''\">update_by = #{updateBy},</if>"
			+ "update_time = sysdate()" + "</set>" + "where post_id = #{postId}</script>")
	public int updatePost(Position post);

	/**
	 * 新增岗位信息
	 * 
	 * @param post 岗位信息
	 * @return 结果
	 */
	@Insert("<script>insert into t_position(" + "<if test=\"postId != null and postId != 0\">post_id,</if>"
			+ "<if test=\"postCode != null and postCode != ''\">post_code,</if>"
			+ "<if test=\"postName != null and postName != ''\">post_name,</if>"
			+ "<if test=\"postSort != null and postSort != ''\">post_sort,</if>"
			+ "<if test=\"status != null and status != ''\">status,</if>"
			+ "<if test=\"remark != null and remark != ''\">remark,</if>"
			+ "<if test=\"createBy != null and createBy != ''\">create_by,</if>" + "create_time" + ")values("
			+ "<if test=\"postId != null and postId != 0\">#{postId},</if>"
			+ "<if test=\"postCode != null and postCode != ''\">#{postCode},</if>"
			+ "<if test=\"postName != null and postName != ''\">#{postName},</if>"
			+ "<if test=\"postSort != null and postSort != ''\">#{postSort},</if>"
			+ "<if test=\"status != null and status != ''\">#{status},</if>"
			+ "<if test=\"remark != null and remark != ''\">#{remark},</if>"
			+ "<if test=\"createBy != null and createBy != ''\">#{createBy},</if>" + "sysdate()" + ")</script>")
	@Options(useGeneratedKeys = true, keyProperty = "postId")
	public int insertPost(Position post);

	/**
	 * 校验岗位名称
	 * 
	 * @param postName 岗位名称
	 * @return 结果
	 */
	@Select("select post_id, post_code, post_name, post_sort, status, create_by, create_time, remark from t_position where post_name=#{postName} limit 1")
	public Position checkPostNameUnique(String postName);

	/**
	 * 校验岗位编码
	 * 
	 * @param postCode 岗位编码
	 * @return 结果
	 */
	@Select("select post_id, post_code, post_name, post_sort, status, create_by, create_time, remark from t_position where post_code=#{postCode} limit 1")
	public Position checkPostCodeUnique(String postCode);
}
